package com.hewei.service;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.hewei.domain.Girl;
import com.hewei.enums.ResultEnum;
import com.hewei.exception.GirlException;
import com.hewei.repository.GirlRepository;

@Service
public class GirlService {
	@Autowired
	private GirlRepository girlRepository;
	
	@Transactional
	public void insertTwo() {
		Girl girlA = new Girl();
		girlA.setCupsize("A");
		girlA.setAge(18);
		girlRepository.save(girlA);
		
		Girl girlB = new Girl();
		girlB.setCupsize("CC");
		girlB.setAge(20);
		girlRepository.save(girlB);
	}
	
	public void getAge(Integer id) throws Exception{
		Girl girl = girlRepository.findOne(id);
		Integer age = girl.getAge();
		if (age < 10) {
			throw new GirlException(ResultEnum.PRIMARY_SCHOOL);
		} else if(age > 10 && age < 16) {
			throw new GirlException(ResultEnum.MIDDLE_SCHOOL);
		}
	}
	
	/**
     * 通过Id查询一个女生的信息
     * @param id
     * @return
     */
    public Girl findOne(Integer id) {
        return girlRepository.findOne(id);
    }
}
